package com.xl.runner;

import com.xl.client.WSClient;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.websocket.ContainerProvider;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;
import java.net.URI;


@Component
public class MyRunner implements ApplicationRunner {

    private static Session session = null;

    @Override
    public void run(ApplicationArguments args) throws Exception {

        //项目启动的时候去连接ws服务端
        System.out.println("Runner");

        URI uri = new URI("ws://localhost:8300/socket");
        WebSocketContainer container = ContainerProvider.getWebSocketContainer();

        Session session = container.connectToServer(WSClient.class, uri);

        //建立了连接之后可以使用session去发送消息
        session.getBasicRemote().sendText("你好我是客户端");


    }

}
